package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.IDataCenterStudentService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.educational.vo.DataCenterStudent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @Author: weiwenfeng
 * @Date: 2018/11/23
 */
/*@EventHandler(tables = {"yyfn.student"})
@Component*/
public class StudentToDataCenterUpdater implements IEventHandler {


    @Resource
    private IStudentService studentService;

    @Resource
    private IDataCenterStudentService dataCenterStudentService ;

    private final Logger log = LoggerFactory.getLogger(StudentToDataCenterUpdater.class);

    @Override
    public void insert(String tableName, JSONArray rowList) {
        for (int i = 0; i < rowList.size(); i++) {
            JSONObject jsonObject = rowList.getJSONObject(i);
            Student student = jsonObject.toJavaObject(Student.class);
            insertStudent(student);
        }
    }

    private void insertStudent(Student student) {
        if (StringUtils.isNotEmpty(student.getCid())){
            changeOrNew(student);
        }else {
            DataCenterStudent dataCenterStudent = new DataCenterStudent(student);
            if( dataCenterStudentService.insertStudent(dataCenterStudent) ){
                Student update = new Student();
                update.setStudentId(student.getStudentId());
                update.setCid(dataCenterStudent.getCid());
                update.setUpdateTime(TimeUtils.now());
                studentService.updateSelectiveById(update);
            }else{
                log.error("插入学生库失败: {} " , JSONObject.toJSONString(student));
                throw new BusException("插入学生库失败");
            }
        }

    }

    private void updateStudent(Student student){
        DataCenterStudent dataCenterStudent = new DataCenterStudent(student);
        if (!dataCenterStudentService.updateStudent(dataCenterStudent)) {
            log.error("更新学生库失败: {} ", JSONObject.toJSONString(student));
            throw new BusException("更新学生库失败");
        }
    }
    private void changeOrNew(Student student){
        DataCenterStudent dataCenterStudent = new DataCenterStudent(student);
        if (!dataCenterStudentService.changeOrNew(dataCenterStudent)) {
            log.error("更新或者新增学生库失败: {} ", JSONObject.toJSONString(student));
            throw new BusException("更新或者新增学生库失败");
        }
    }



    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        for (int i = 0; i < rowList.size(); i++) {
            JSONObject jsonObject = rowList.getJSONObject(i);
            Student student = jsonObject.toJavaObject(Student.class);
            if (StringUtils.isEmpty(student.getCid())) {
                insertStudent(student);
            } else {
                updateStudent(student);
            }
        }
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }


}
